package cn.kgc.springmvc02.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: mosin
 * @version: v1.0  2022/12/13
 * springmvc中异常的处理机制
 */
@Controller
@RequestMapping("ex")
public class MyExceptionController {

    // 当前类的方法出现数学运算异常时 调用该方法
    //@ExceptionHandler({ArithmeticException.class,NullPointerException.class})
    //@ExceptionHandler(ArithmeticException.class)
    //public String  arithmeticExceptionHandler(ArithmeticException e){
    //    e.printStackTrace();
    //    return "err";
    //}


    //@ExceptionHandler(NullPointerException.class)
    //public String  nullPointerExceptionHandler(NullPointerException e){
    //    e.printStackTrace();
    //    return "err2";
    //}

    //@ExceptionHandler(NullPointerException.class)
    //@ResponseBody
    //public Map nullPointerExceptionHandler(NullPointerException e){
    //    e.printStackTrace();
    //    HashMap<String, Object> map = new HashMap<>();
    //    map.put("code", 500);
    //    map.put("msg", "空指针异常");
    //    return map;
    //}


    @RequestMapping("ex1")
    public String  method1(){
        //int a = 1/0;
        return "index";
    }


    @RequestMapping("ex2")
    public String  method2(){
        String str = null;
        str.length();
        return "index";
    }

}
